home *** CD-ROM | disk | FTP | other *** search
/ Workbench Add-On / Workbench Add-On - Volume 1.iso / BBS-Archive / Comm / term-source.lha / Extras / Source / term-Source.lha / termXEM.c < prev    next >
C/C++ Source or Header  |  1995-06-06  |  23KB  |  1,231 lines

  1. /*
  2. **    termXEM.c
  3. **
  4. **    External emulation support routines
  5. **
  6. **    Copyright © 1990-1995 by Olaf `Olsen' Barthel
  7. **        All Rights Reserved
  8. */
  9.  
  10. #include "termGlobal.h"
  11.  
  12.     /* GetOptionMode(struct xpr_option *Option):
  13.      *
  14.      *    Turn text into a boolean value.
  15.      */
  16.  
  17. STATIC BYTE __regargs
  18. GetOptionMode(struct xem_option *Option)
  19. {
  20.     if(Option)
  21.     {
  22.         STATIC STRPTR TrueOptions[] =
  23.         {
  24.             "ON",
  25.             "TRUE",
  26.             "T",
  27.             "YES",
  28.             "Y",
  29.             NULL
  30.         };
  31.  
  32.         register WORD i;
  33.  
  34.         for(i = 0 ; TrueOptions[i] ; i++)
  35.         {
  36.             if(!Stricmp(Option -> xemo_value,TrueOptions[i]))
  37.                 return(TRUE);
  38.         }
  39.     }
  40.  
  41.     return(FALSE);
  42. }
  43.  
  44. STATIC LONG __saveds __asm
  45. xem_sflush(VOID)
  46. {
  47.     return((LONG)DoSerialCmd(CMD_CLEAR));
  48. }
  49.  
  50. STATIC LONG __saveds __asm
  51. xem_squery(VOID)
  52. {
  53.     if(WriteRequest)
  54.     {
  55.         ULONG    Waiting;
  56.         UWORD    Status;
  57.  
  58.         GetSerialInfo(&Waiting,&Status);
  59.  
  60.             /* Return error if carrier is lost. */
  61.  
  62.         if((Status & CIAF_COMCD) && Config -> SerialConfig -> CheckCarrier && !Config -> SerialConfig -> DirectConnection)
  63.         {
  64.             ObtainSemaphore(&OnlineSemaphore);
  65.  
  66.             if(Online)
  67.             {
  68.                 WasOnline    = Online;
  69.                 Online        = FALSE;
  70.             }
  71.  
  72.             ReleaseSemaphore(&OnlineSemaphore);
  73.         }
  74.         else
  75.             return((LONG)Waiting);
  76.     }
  77.  
  78.     return(-1);
  79. }
  80.  
  81. STATIC LONG __saveds __asm
  82. xem_sread(register __a0 APTR Buffer,register __d0 LONG Size,register __d1 ULONG Timeout)
  83. {
  84.         /* Are both IORequests available? */
  85.  
  86.     if(WriteRequest && ReadRequest)
  87.     {
  88.             /* Valid size parameter? */
  89.  
  90.         if(Size > 0)
  91.         {
  92.             ULONG    Waiting;
  93.             UWORD    Status;
  94.  
  95.             GetSerialInfo(&Waiting,&Status);
  96.  
  97.                 /* Return error if carrier is lost. */
  98.  
  99.             if(Config -> SerialConfig -> CheckCarrier && !Config -> SerialConfig -> DirectConnection)
  100.             {
  101.                 if(Status & CIAF_COMCD)
  102.                 {
  103.                     ObtainSemaphore(&OnlineSemaphore);
  104.  
  105.                     if(Online)
  106.                     {
  107.                         WasOnline    = Online;
  108.                         Online        = FALSE;
  109.                     }
  110.  
  111.                     ReleaseSemaphore(&OnlineSemaphore);
  112.  
  113.                     return(-1);
  114.                 }
  115.             }
  116.  
  117.             /* ALWAYS */
  118.             {
  119.                 if(Waiting)
  120.                 {
  121.                         /* No timeout specified? Read as many
  122.                          * bytes as available.
  123.                          */
  124.  
  125.                     if(!Timeout)
  126.                     {
  127.                         if(Waiting > Size)
  128.                             Waiting = Size;
  129.  
  130.                         if(DoSerialRead(Buffer,Waiting))
  131.                             Waiting = ReadRequest -> IOSer . io_Actual;
  132.  
  133.                         BytesIn += Waiting;
  134.  
  135.                         return((LONG)Waiting);
  136.                     }
  137.  
  138.                         /* Enough data pending to be read? */
  139.  
  140.                     if(Waiting >= Size)
  141.                     {
  142.                         if(DoSerialRead(Buffer,Size))
  143.                             Size = ReadRequest -> IOSer . io_Actual;
  144.  
  145.                         BytesIn += Size;
  146.  
  147.                         return(Size);
  148.                     }
  149.                 }
  150.                 else
  151.                 {
  152.                         /* No timeout & no bytes available:
  153.                          * return immediately.
  154.                          */
  155.  
  156.                     if(!Timeout)
  157.                         return(0);
  158.                 }
  159.             }
  160.  
  161.             /* ALWAYS */
  162.             {
  163.                 register ULONG SignalSet;
  164.  
  165.                     /* Set up the timer. */
  166.  
  167.                 TimeRequest -> tr_node . io_Command    = TR_ADDREQUEST;
  168.                 TimeRequest -> tr_time . tv_secs    = Timeout / MILLION;
  169.                 TimeRequest -> tr_time . tv_micro    = Timeout % MILLION;
  170.  
  171.                     /* Prevent early termination. */
  172.  
  173.                 ClrSignal(SIG_SERIAL | SIG_TIMER);
  174.  
  175.                     /* Start IO... */
  176.  
  177.                 SendIO(TimeRequest);
  178.  
  179.                     /* Set up the read request. */
  180.  
  181.                 StartSerialRead(Buffer,Size);
  182.  
  183.                 FOREVER
  184.                 {
  185.                     SignalSet = Wait(SIG_SERIAL | SIG_TIMER);
  186.  
  187.                         /* Receive buffer filled? */
  188.  
  189.                     if(SignalSet & SIG_SERIAL)
  190.                     {
  191.                             /* Abort the timer request. */
  192.  
  193.                         if(!CheckIO(TimeRequest))
  194.                             AbortIO(TimeRequest);
  195.  
  196.                         WaitIO(TimeRequest);
  197.  
  198.                             /* Did the request terminate gracefully? */
  199.  
  200.                         if(WaitSerialRead())
  201.                             Size = ReadRequest -> IOSer . io_Actual;
  202.  
  203.                         BytesIn += Size;
  204.  
  205.                         return(Size);
  206.                     }
  207.  
  208.                         /* Hit by timeout? */
  209.  
  210.                     if(SignalSet & SIG_TIMER)
  211.                     {
  212.                             /* Abort the read request. */
  213.  
  214.                         StopSerialRead();
  215.  
  216.                             /* Remove the timer request. */
  217.  
  218.                         WaitIO(TimeRequest);
  219.  
  220.                             /* Did the driver receive any
  221.                              * data?
  222.                              */
  223.  
  224.                         if(ReadRequest -> IOSer . io_Actual)
  225.                         {
  226.                             BytesIn += ReadRequest -> IOSer . io_Actual;
  227.  
  228.                             return((LONG)ReadRequest -> IOSer . io_Actual);
  229.                         }
  230.                         else
  231.                         {
  232.                                 /* Take a second look and query the number of
  233.                                  * bytes ready to be received, there may
  234.                                  * still be some bytes in the buffer.
  235.                                  * Note: this depends on the way the
  236.                                  * driver handles read abort.
  237.                                  */
  238.  
  239.                             Waiting = GetSerialWaiting();
  240.  
  241.                                 /* Don't read too much. */
  242.  
  243.                             if(Size > Waiting)
  244.                                 Size = Waiting;
  245.  
  246.                                 /* Are there any bytes to be transferred? */
  247.  
  248.                             if(Size)
  249.                             {
  250.                                     /* Read the data. */
  251.  
  252.                                 if(DoSerialRead(Buffer,Size))
  253.                                     Size = ReadRequest -> IOSer . io_Actual;
  254.  
  255.                                 BytesIn += Size;
  256.                             }
  257.  
  258.                             return(Size);
  259.                         }
  260.                     }
  261.                 }
  262.             }
  263.         }
  264.         else
  265.             return(0);
  266.     }
  267.     else
  268.         return(-1);
  269. }
  270.  
  271. STATIC ULONG __saveds __asm
  272. xem_toptions(register __d0 LONG NumOpts,register __a0 struct xem_option **Opts)
  273. {
  274.     if(NumOpts && Opts)
  275.     {
  276.         enum    {    GAD_USE=1,GAD_CANCEL,GAD_SPECIAL };
  277.  
  278.         struct LayoutHandle    *Handle;
  279.         ULONG             Flags = NULL;
  280.  
  281.             /* We only have 32 bits! */
  282.  
  283.         if(NumOpts > 32)
  284.             NumOpts = 32;
  285.  
  286.         if(Handle = LT_CreateHandleTags(Window -> WScreen,
  287.             LH_LocaleHook,    &LocaleHook,
  288.         TAG_DONE))
  289.         {
  290.             struct Window    *PanelWindow;
  291.             LONG         i,Split;
  292.  
  293.             if(NumOpts > 16)
  294.                 Split = NumOpts / 2;
  295.             else
  296.                 Split = -1;
  297.  
  298.             LT_New(Handle,
  299.                 LA_Type,    VERTICAL_KIND,
  300.             TAG_DONE);
  301.             {
  302.                 LT_New(Handle,
  303.                     LA_Type,    HORIZONTAL_KIND,
  304.                     LA_LabelID,    MSG_V36_1501,
  305.                 TAG_DONE);
  306.                 {
  307.                     LT_New(Handle,
  308.                         LA_Type,    VERTICAL_KIND,
  309.                     TAG_DONE);
  310.                     {
  311.                         for(i = 0 ; i < NumOpts ; i++)
  312.                         {
  313.                             if(Opts[i])
  314.                             {
  315.                                 switch(Opts[i] -> xemo_type)
  316.                                 {
  317.                                     case XEMO_BOOLEAN:
  318.  
  319.                                         LT_New(Handle,
  320.                                             LA_Type,    CHECKBOX_KIND,
  321.                                             LA_LabelText,    Opts[i] -> xemo_description,
  322.                                             LA_ID,        GAD_SPECIAL + i,
  323.                                             GTCB_Checked,    GetOptionMode(Opts[i]),
  324.                                         TAG_DONE);
  325.  
  326.                                         break;
  327.  
  328.                                     case XEMO_LONG:
  329.  
  330.                                         LT_New(Handle,
  331.                                             LA_Type,        INTEGER_KIND,
  332.                                             LA_LabelText,        Opts[i] -> xemo_description,
  333.                                             LA_ID,            GAD_SPECIAL + i,
  334.                                             LA_Chars,        15,
  335.                                             GTIN_Number,        Atol(Opts[i] -> xemo_value),
  336.                                             LAIN_UseIncrementers,    TRUE,
  337.                                         TAG_DONE);
  338.  
  339.                                         break;
  340.  
  341.                                     case XEMO_STRING:
  342.  
  343.                                         LT_New(Handle,
  344.                                             LA_Type,    STRING_KIND,
  345.                                             LA_LabelText,    Opts[i] -> xemo_description,
  346.                                             LA_ID,        GAD_SPECIAL + i,
  347.                                             LA_Chars,    15,
  348.                                             GTST_String,    Opts[i] -> xemo_value,
  349.                                             GTST_MaxChars,    Opts[i] -> xemo_length - 1,
  350.                                         TAG_DONE);
  351.  
  352.                                         break;
  353.  
  354.                                     case XEMO_COMMPAR:
  355.  
  356.                                         LT_New(Handle,
  357.                                             LA_Type,    STRING_KIND,
  358.                                             LA_LabelText,    Opts[i] -> xemo_description,
  359.                                             LA_ID,        GAD_SPECIAL + i,
  360.                                             LA_Chars,    15,
  361.                                             LA_HighLabel,    TRUE,
  362.                                             GTST_String,    Opts[i] -> xemo_value,
  363.                                             GTST_MaxChars,    Opts[i] -> xemo_length - 1,
  364.                                         TAG_DONE);
  365.  
  366.                                         break;
  367.  
  368.                                     case XEMO_HEADER:
  369.  
  370.                                         LT_New(Handle,
  371.                                             LA_Type,    TEXT_KIND,
  372.                                             LA_LabelText,    Opts[i] -> xemo_description,
  373.                                             LA_HighLabel,    TRUE,
  374.                                             GTTX_Text,    " ",
  375.                                         TAG_DONE);
  376.  
  377.                                         break;
  378.  
  379.                                     case XEMO_COMMAND:
  380.  
  381.                                         LT_New(Handle,
  382.                                             LA_Type,    BUTTON_KIND,
  383.                                             LA_LabelText,    Opts[i] -> xemo_description,
  384.                                             LA_ID,        GAD_SPECIAL + i,
  385.                                             LA_Chars,    15,
  386.                                         TAG_DONE);
  387.  
  388.                                         break;
  389.                                 }
  390.                             }
  391.  
  392.                             if(i == Split)
  393.                             {
  394.                                 LT_EndGroup(Handle);
  395.  
  396.                                 LT_New(Handle,
  397.                                     LA_Type,    VERTICAL_KIND,
  398.                                 TAG_DONE);
  399.                             }
  400.                         }
  401.  
  402.                         LT_EndGroup(Handle);
  403.                     }
  404.  
  405.                     LT_EndGroup(Handle);
  406.                 }
  407.  
  408.                 LT_New(Handle,
  409.                     LA_Type,    VERTICAL_KIND,
  410.                 TAG_DONE);
  411.                 {
  412.                     LT_New(Handle,LA_Type,XBAR_KIND,LAXB_FullSize,TRUE,TAG_DONE);
  413.  
  414.                     LT_EndGroup(Handle);
  415.                 }
  416.  
  417.                 LT_New(Handle,LA_Type,HORIZONTAL_KIND,
  418.                     LAGR_SameSize,    TRUE,
  419.                     LAGR_Spread,    TRUE,
  420.                 TAG_DONE);
  421.                 {
  422.                     LT_New(Handle,
  423.                         LA_Type,    BUTTON_KIND,
  424.                         LA_LabelID,    MSG_GLOBAL_USE_GAD,
  425.                         LA_ID,        GAD_USE,
  426.                         LABT_ReturnKey,    TRUE,
  427.                         LABT_ExtraFat,    TRUE,
  428.                     TAG_DONE);
  429.  
  430.                     LT_New(Handle,
  431.                         LA_Type,    BUTTON_KIND,
  432.                         LA_LabelID,    MSG_GLOBAL_CANCEL_GAD,
  433.                         LA_ID,        GAD_CANCEL,
  434.                         LABT_EscKey,    TRUE,
  435.                         LABT_ExtraFat,    TRUE,
  436.                     TAG_DONE);
  437.  
  438.                     LT_EndGroup(Handle);
  439.                 }
  440.  
  441.                 LT_EndGroup(Handle);
  442.             }
  443.  
  444.             if(PanelWindow = LT_Layout(Handle,OptionTitle ? OptionTitle : LocaleString(MSG_V36_1840),NULL,0,0,IDCMP_CLOSEWINDOW,0,
  445.                 LAWN_HelpHook,        &GuideHook,
  446.                 LAWN_Parent,        Window,
  447.                 WA_DepthGadget,        TRUE,
  448.                 WA_CloseGadget,        TRUE,
  449.                 WA_DragBar,        TRUE,
  450.                 WA_RMBTrap,        TRUE,
  451.                 WA_Activate,        TRUE,
  452.             TAG_DONE))
  453.             {
  454.                 struct IntuiMessage    *Message;
  455.                 BOOLEAN             Done = FALSE;
  456.                 ULONG             MsgClass,
  457.                              MsgQualifier;
  458.                 UWORD             MsgCode;
  459.                 struct Gadget        *MsgGadget;
  460.                 BOOLEAN             CheckFlags = FALSE;
  461.  
  462.                 PushWindow(PanelWindow);
  463.  
  464.                 LT_ShowWindow(Handle,TRUE);
  465.  
  466.                 do
  467.                 {
  468.                     if(Wait(PORTMASK(PanelWindow -> UserPort) | SIG_BREAK) & SIG_BREAK)
  469.                         break;
  470.  
  471.                     while(Message = (struct IntuiMessage *)GT_GetIMsg(PanelWindow -> UserPort))
  472.                     {
  473.                         MsgClass    = Message -> Class;
  474.                         MsgQualifier    = Message -> Qualifier;
  475.                         MsgCode        = Message -> Code;
  476.                         MsgGadget    = (struct Gadget *)Message -> IAddress;
  477.  
  478.                         GT_ReplyIMsg(Message);
  479.  
  480.                         LT_HandleInput(Handle,MsgQualifier,&MsgClass,&MsgCode,&MsgGadget);
  481.  
  482.                         if(MsgClass == IDCMP_CLOSEWINDOW)
  483.                             Done = TRUE;
  484.  
  485.                         if(MsgClass == IDCMP_GADGETUP)
  486.                         {
  487.                             switch(MsgGadget -> GadgetID)
  488.                             {
  489.                                 case GAD_USE:
  490.  
  491.                                     LT_UpdateStrings(Handle);
  492.  
  493.                                     Done = CheckFlags = TRUE;
  494.                                     break;
  495.  
  496.                                 case GAD_CANCEL:
  497.  
  498.                                     Done = TRUE;
  499.                                     break;
  500.  
  501.                                 default:
  502.  
  503.                                     if(MsgGadget -> GadgetID - GAD_SPECIAL < NumOpts)
  504.                                     {
  505.                                         i = MsgGadget -> GadgetID - GAD_SPECIAL;
  506.  
  507.                                         if(Opts[i] -> xemo_type == XEMO_COMMAND || (Opts[i] -> xemo_type == XEMO_COMMPAR && MsgCode != '\t'))
  508.                                         {
  509.                                             Flags = (1L << i);
  510.  
  511.                                             Done = CheckFlags = TRUE;
  512.                                         }
  513.                                     }
  514.  
  515.                                     break;
  516.                             }
  517.                         }
  518.                     }
  519.                 }
  520.                 while(!Done);
  521.  
  522.                 PopWindow();
  523.  
  524.                 if(CheckFlags)
  525.                 {
  526.                     STRPTR String;
  527.  
  528.                     LT_LockWindow(PanelWindow);
  529.  
  530.                     for(i = 0 ; i < NumOpts ; i++)
  531.                     {
  532.                         if(Opts[i])
  533.                         {
  534.                             switch(Opts[i] -> xemo_type)
  535.                             {
  536.                                 case XEMO_BOOLEAN:
  537.  
  538.                                     if(LT_GetAttributes(Handle,GAD_SPECIAL + i,TAG_DONE) != GetOptionMode(Opts[i]))
  539.                                     {
  540.                                         Flags |= (1L << i);
  541.  
  542.                                         if(LT_GetAttributes(Handle,GAD_SPECIAL + i,TAG_DONE))
  543.                                             strcpy(Opts[i] -> xemo_value,"yes");
  544.                                         else
  545.                                             strcpy(Opts[i] -> xemo_value,"no");
  546.  
  547.                                         NewOptions = TRUE;
  548.                                     }
  549.  
  550.                                     break;
  551.  
  552.                                 case XEMO_LONG:
  553.  
  554.                                     if(Atol(Opts[i] -> xemo_value) != LT_GetAttributes(Handle,GAD_SPECIAL + i,TAG_DONE))
  555.                                     {
  556.                                         Flags |= (1L << i);
  557.  
  558.                                         SPrintf(Opts[i] -> xemo_value,"%ld",LT_GetAttributes(Handle,GAD_SPECIAL + i,TAG_DONE));
  559.  
  560.                                         NewOptions = TRUE;
  561.                                     }
  562.  
  563.                                     break;
  564.  
  565.                                 case XEMO_COMMPAR:
  566.                                 case XEMO_STRING:
  567.  
  568.                                     if(String = (STRPTR)LT_GetAttributes(Handle,GAD_SPECIAL + i,TAG_DONE))
  569.                                     {
  570.                                         if(strcmp(Opts[i] -> xemo_value,String))
  571.                                         {
  572.                                             Flags |= (1L << i);
  573.  
  574.                                             strcpy(Opts[i] -> xemo_value,(STRPTR)LT_GetAttributes(Handle,GAD_SPECIAL + i,TAG_DONE));
  575.  
  576.                                             NewOptions = TRUE;
  577.                                         }
  578.                                     }
  579.  
  580.                                     break;
  581.                             }
  582.                         }
  583.                     }
  584.  
  585.                     LT_UnlockWindow(PanelWindow);
  586.                 }
  587.                 else
  588.                     Flags = NULL;
  589.             }
  590.  
  591.             LT_DeleteHandle(Handle);
  592.         }
  593.  
  594.         return(Flags);
  595.     }
  596.     else
  597.         return(NULL);
  598. }
  599.  
  600.     /* xem_swrite():
  601.      *
  602.      *    Send a few bytes across the serial line.
  603.      */
  604.  
  605. STATIC LONG __saveds __asm
  606. xem_swrite(register __a0 STRPTR Buffer,register __d0 LONG Size)
  607. {
  608.     if(WriteRequest)
  609.     {
  610.         SerWrite(Buffer,Size);
  611.  
  612.         return(0);
  613.     }
  614.     else
  615.         return(-1);
  616. }
  617.  
  618.     /* xem_sbreak():
  619.      *
  620.      *    Send a break signal across the serial line.
  621.      */
  622.  
  623. STATIC LONG __asm __saveds
  624. xem_sbreak(VOID)
  625. {
  626.     if(!WriteRequest)
  627.         return(-1);
  628.     else
  629.     {
  630.         SendBreak();
  631.  
  632.         return(0);
  633.     }
  634. }
  635.  
  636.     /* xem_sstart():
  637.      *
  638.      *    Restart serial read activity.
  639.      */
  640.  
  641. STATIC VOID __asm __saveds
  642. xem_sstart(VOID)
  643. {
  644.     RestartSerial(FALSE);
  645. }
  646.  
  647.     /* xem_sstop():
  648.      *
  649.      *    Stop serial read activity.
  650.      */
  651.  
  652. STATIC LONG __asm __saveds
  653. xem_sstop(VOID)
  654. {
  655.     StopSerialRead();
  656.  
  657.     return(0);
  658. }
  659.  
  660.     /* xem_tgets(STRPTR Prompt,STRPTR Buffer,ULONG Size):
  661.      *
  662.      *    Get a string from the user.
  663.      */
  664.  
  665. STATIC LONG __saveds __asm
  666. xem_tgets(register __a0 STRPTR Prompt,register __a1 STRPTR Buffer,register __d0 ULONG Size)
  667. {
  668.     enum    {    GAD_OK=1,GAD_CANCEL,GAD_STRING };
  669.  
  670.     struct LayoutHandle    *Handle;
  671.     LONG             Success = FALSE;
  672.     UBYTE             LocalBuffer[256];
  673.  
  674.     if(strlen(Buffer) > 255)
  675.     {
  676.         CopyMem(Buffer,LocalBuffer,255);
  677.  
  678.         LocalBuffer[255] = 0;
  679.     }
  680.     else
  681.         strcpy(LocalBuffer,Buffer);
  682.  
  683.     if(!Prompt)
  684.         Prompt = LocaleString(MSG_TERMXPR_INPUT_REQUIRED_TXT);
  685.  
  686.     if(Handle = LT_CreateHandleTags(Window -> WScreen,
  687.         LH_LocaleHook,    &LocaleHook,
  688.     TAG_DONE))
  689.     {
  690.         struct Window *PanelWindow;
  691.  
  692.         LT_New(Handle,
  693.             LA_Type,    VERTICAL_KIND,
  694.         TAG_DONE);
  695.         {
  696.             LT_New(Handle,
  697.                 LA_Type,    VERTICAL_KIND,
  698.                 LA_LabelText,    Prompt,
  699.             TAG_DONE);
  700.             {
  701.                 LT_New(Handle,
  702.                     LA_Type,    STRING_KIND,
  703.                     LA_STRPTR,    LocalBuffer,
  704.                     LA_Chars,    30,
  705.                     GTST_MaxChars,    Size,
  706.                 TAG_DONE);
  707.  
  708.                 LT_EndGroup(Handle);
  709.             }
  710.  
  711.             LT_New(Handle,
  712.                 LA_Type,VERTICAL_KIND,
  713.             TAG_DONE);
  714.             {
  715.                 LT_New(Handle,
  716.                     LA_Type,    XBAR_KIND,
  717.                     LAXB_FullSize,    TRUE,
  718.                 TAG_DONE);
  719.  
  720.                 LT_EndGroup(Handle);
  721.             }
  722.  
  723.             LT_New(Handle,LA_Type,HORIZONTAL_KIND,
  724.                 LAGR_SameSize,    TRUE,
  725.                 LAGR_Spread,    TRUE,
  726.             TAG_DONE);
  727.             {
  728.                 LT_New(Handle,
  729.                     LA_Type,    BUTTON_KIND,
  730.                     LA_LabelID,    MSG_TERMXPR_OKAY_GAD,
  731.                     LA_ID,        GAD_OK,
  732.                     LABT_ReturnKey,    TRUE,
  733.                     LABT_ExtraFat,    TRUE,
  734.                 TAG_DONE);
  735.  
  736.                 LT_New(Handle,
  737.                     LA_Type,    BUTTON_KIND,
  738.                     LA_LabelID,    MSG_GLOBAL_CANCEL_GAD,
  739.                     LA_ID,        GAD_CANCEL,
  740.                     LABT_EscKey,    TRUE,
  741.                     LABT_ExtraFat,    TRUE,
  742.                 TAG_DONE);
  743.  
  744.                 LT_EndGroup(Handle);
  745.             }
  746.         }
  747.  
  748.         if(PanelWindow = LT_Layout(Handle,LocaleString(MSG_GLOBAL_ENTER_TEXT_TXT),NULL,0,0,IDCMP_CLOSEWINDOW,0,
  749.             LAWN_HelpHook,        &GuideHook,
  750.             LAWN_Parent,        Window,
  751.             WA_DepthGadget,        TRUE,
  752.             WA_CloseGadget,        TRUE,
  753.             WA_DragBar,        TRUE,
  754.             WA_RMBTrap,        TRUE,
  755.             WA_Activate,        TRUE,
  756.         TAG_DONE))
  757.         {
  758.             struct IntuiMessage    *Message;
  759.             BOOLEAN             Done = FALSE;
  760.             ULONG             MsgClass,
  761.                          MsgQualifier;
  762.             UWORD             MsgCode;
  763.             struct Gadget        *MsgGadget;
  764.  
  765.             PushWindow(PanelWindow);
  766.  
  767.             LT_ShowWindow(Handle,TRUE);
  768.  
  769.             LT_Activate(Handle,GAD_STRING);
  770.  
  771.             do
  772.             {
  773.                 if(Wait(PORTMASK(PanelWindow -> UserPort) | SIG_BREAK) & SIG_BREAK)
  774.                     break;
  775.  
  776.                 while(Message = (struct IntuiMessage *)GT_GetIMsg(PanelWindow -> UserPort))
  777.                 {
  778.                     MsgClass    = Message -> Class;
  779.                     MsgQualifier    = Message -> Qualifier;
  780.                     MsgCode        = Message -> Code;
  781.                     MsgGadget    = (struct Gadget *)Message -> IAddress;
  782.  
  783.                     GT_ReplyIMsg(Message);
  784.  
  785.                     LT_HandleInput(Handle,MsgQualifier,&MsgClass,&MsgCode,&MsgGadget);
  786.  
  787.                     if(MsgClass == IDCMP_CLOSEWINDOW)
  788.                         Done = TRUE;
  789.  
  790.                     if(MsgClass == IDCMP_GADGETUP)
  791.                     {
  792.                         switch(MsgGadget -> GadgetID)
  793.                         {
  794.                             case GAD_STRING:
  795.  
  796.                                 if(MsgCode == '\r')
  797.                                 {
  798.                                     strcpy(Buffer,LocalBuffer);
  799.  
  800.                                     Success = Done = TRUE;
  801.  
  802.                                     LT_PressButton(Handle,GAD_OK);
  803.                                 }
  804.  
  805.                                 break;
  806.  
  807.                             case GAD_OK:
  808.  
  809.                                 strcpy(Buffer,LocalBuffer);
  810.  
  811.                                 Success = Done = TRUE;
  812.                                 break;
  813.  
  814.                             case GAD_CANCEL:
  815.  
  816.                                 Done = TRUE;
  817.                                 break;
  818.                         }
  819.                     }
  820.                 }
  821.             }
  822.             while(!Done);
  823.  
  824.             PopWindow();
  825.         }
  826.  
  827.         LT_DeleteHandle(Handle);
  828.     }
  829.  
  830.     return(Success);
  831. }
  832.  
  833.     /* xem_tbeep(ULONG Times,ULONG Delay):
  834.      *
  835.      *    Beep the terminal display.
  836.      */
  837.  
  838. STATIC VOID __saveds __asm
  839. xem_tbeep(register __d0 ULONG Times,register __d1 ULONG Delay)
  840. {
  841.     WORD i;
  842.  
  843.     for(i = 0 ; i < Times ; i++)
  844.     {
  845.             /* Handle the visual part. */
  846.  
  847.         if(Config -> TerminalConfig -> BellMode != BELL_AUDIBLE)
  848.         {
  849.             if(StatusProcess)
  850.                 Signal(StatusProcess,SIG_BELL);
  851.         }
  852.  
  853.             /* Let it beep. */
  854.  
  855.         if(Config -> TerminalConfig -> BellMode == BELL_AUDIBLE || Config -> TerminalConfig -> BellMode == BELL_BOTH)
  856.             SoundPlay(SOUND_BELL);
  857.     }
  858. }
  859.  
  860.     /* xem_macrodispatch(struct XEmulatorMacroKey *XEM_MacroKey):
  861.      *
  862.      *    Dispatch a macro key call.
  863.      */
  864.  
  865. STATIC LONG __saveds __asm
  866. xem_macrodispatch(register __a0 struct XEmulatorMacroKey *XEM_MacroKey)
  867. {
  868.     VOID (*Routine)(VOID);
  869.  
  870.         /* If a routine to call is available (most likely xON or xOFF),
  871.          * make a call to it, else process the macro key data.
  872.          */
  873.  
  874.     if(Routine = (VPTR)XEM_MacroKey -> xmk_UserData)
  875.         (*Routine)();
  876.     else
  877.         SerialCommand(MacroKeys -> Keys[XEM_MacroKey -> xmk_Qualifier][XEM_MacroKey -> xmk_Code - 0x50]);
  878.  
  879.     return(0);
  880. }
  881.  
  882.     /* SetEmulatorOptions(BYTE Mode):
  883.      *
  884.      *    Save or load the emulator options.
  885.      */
  886.  
  887. BYTE __regargs
  888. SetEmulatorOptions(BYTE Mode)
  889. {
  890.     BYTE Success = FALSE;
  891.  
  892.         /* Is the library available and running? */
  893.  
  894.     if(XEmulatorBase && XEM_IO)
  895.     {
  896.             /* Are we using the new library code? */
  897.  
  898.         if(XEmulatorBase -> lib_Version >= 4)
  899.         {
  900.                 /* Get the name of the library. */
  901.  
  902.             strcpy(SharedBuffer,FilePart(XEmulatorBase -> lib_Node . ln_Name));
  903.  
  904.                 /* Does it have any name? */
  905.  
  906.             if(SharedBuffer[0])
  907.             {
  908.                 UBYTE    OtherBuffer[50];
  909.                 WORD    i;
  910.  
  911.                     /* Strip the `.library' bit. */
  912.  
  913.                 for(i = strlen(SharedBuffer) - 1 ; i >= 0 ; i--)
  914.                 {
  915.                     if(SharedBuffer[i] == '.')
  916.                     {
  917.                         SharedBuffer[i] = 0;
  918.  
  919.                         break;
  920.                     }
  921.                 }
  922.  
  923.                     /* What are we to do? */
  924.  
  925.                 if(Mode == XEM_PREFS_LOAD)
  926.                 {
  927.                         /* Restore settings... */
  928.  
  929.                     strcpy(OtherBuffer,"ENV:");
  930.  
  931.                     if(AddPart(OtherBuffer,SharedBuffer,50))
  932.                     {
  933.                             /* If we can't load them,
  934.                              * reset to defaults.
  935.                              */
  936.  
  937.                         if(!XEmulatorPreferences(XEM_IO,OtherBuffer,Mode))
  938.                         {
  939.                             strcpy(OtherBuffer,"ENV:xem");
  940.  
  941.                             if(AddPart(OtherBuffer,SharedBuffer,50))
  942.                             {
  943.                                 if(XEmulatorPreferences(XEM_IO,OtherBuffer,Mode))
  944.                                     Success = TRUE;
  945.                             }
  946.                         }
  947.                         else
  948.                             Success = TRUE;
  949.  
  950.                         if(!Success)
  951.                             XEmulatorPreferences(XEM_IO,NULL,XEM_PREFS_RESET);
  952.                     }
  953.                 }
  954.                 else
  955.                 {
  956.                         /* Save settings to ENV: */
  957.  
  958.                     strcpy(OtherBuffer,"ENV:");
  959.  
  960.                     if(AddPart(OtherBuffer,SharedBuffer,50))
  961.                     {
  962.                         if(XEmulatorPreferences(XEM_IO,OtherBuffer,Mode))
  963.                             Success = TRUE;
  964.                     }
  965.  
  966.                     if(Success)
  967.                     {
  968.                         Success = FALSE;
  969.  
  970.                             /* Save settings to ENVARC: */
  971.  
  972.                         strcpy(OtherBuffer,"ENVARC:");
  973.  
  974.                         if(AddPart(OtherBuffer,SharedBuffer,50))
  975.                         {
  976.                             if(XEmulatorPreferences(XEM_IO,OtherBuffer,Mode))
  977.                                 Success = TRUE;
  978.                         }
  979.                     }
  980.                 }
  981.             }
  982.         }
  983.     }
  984.  
  985.         /* Return result. */
  986.  
  987.     return(Success);
  988. }
  989.  
  990.     /* SetupEmulator(BYTE OpenConsole):
  991.      *
  992.      *    Initialize the XEM_IO structure.
  993.      */
  994.  
  995. STATIC BYTE
  996. SetupEmulator(VOID)
  997. {
  998.     if(!XEM_IO)
  999.     {
  1000.         if(XEM_IO = (struct XEM_IO *)AllocVec(sizeof(struct XEM_IO),MEMF_ANY | MEMF_CLEAR | MEMF_PUBLIC))
  1001.         {
  1002.             XEM_IO -> xem_window        = Window;
  1003.             XEM_IO -> xem_font        = CurrentFont;
  1004.             XEM_IO -> xem_signal        = &XEM_Signal;
  1005.             XEM_IO -> xem_screendepth    = GetBitMapDepth(Window -> WScreen -> RastPort . BitMap);
  1006.  
  1007.             XEM_IO -> xem_sread        = xem_sread;
  1008.             XEM_IO -> xem_swrite        = xem_swrite;
  1009.             XEM_IO -> xem_sflush        = xem_sflush;
  1010.             XEM_IO -> xem_sbreak        = xem_sbreak;
  1011.             XEM_IO -> xem_squery        = xem_squery;
  1012.             XEM_IO -> xem_sstart        = xem_sstart;
  1013.             XEM_IO -> xem_sstop        = xem_sstop;
  1014.  
  1015.             XEM_IO -> xem_tbeep        = xem_tbeep;
  1016.             XEM_IO -> xem_tgets        = xem_tgets;
  1017.             XEM_IO -> xem_toptions        = xem_toptions;
  1018.  
  1019.             XEM_IO -> xem_process_macrokeys    = xem_macrodispatch;
  1020.  
  1021.             return(TRUE);
  1022.         }
  1023.     }
  1024.     else
  1025.         return(FALSE);
  1026.  
  1027.     return(FALSE);
  1028. }
  1029.  
  1030.     /* CloseEmulator():
  1031.      *
  1032.      *    Close the emulation library.
  1033.      */
  1034.  
  1035. VOID
  1036. CloseEmulator()
  1037. {
  1038.     if(XEmulatorBase)
  1039.     {
  1040.         if(XEM_IO)
  1041.         {
  1042.             XEmulatorMacroKeyFilter(XEM_IO,NULL);
  1043.             XEmulatorCloseConsole(XEM_IO);
  1044.             XEmulatorCleanup(XEM_IO);
  1045.         }
  1046.  
  1047.         CloseLibrary(XEmulatorBase);
  1048.  
  1049.         if(XEM_IO)
  1050.             FreeVec(XEM_IO);
  1051.  
  1052.         strcpy(EmulationName,LocaleString(MSG_TERMXEM_NO_EMULATION_TXT));
  1053.  
  1054.         XEmulatorBase    = NULL;
  1055.         XEM_Signal    = NULL;
  1056.         XEM_IO        = NULL;
  1057.  
  1058.         RasterEnabled = TRUE;
  1059.  
  1060.         ClearCursor();
  1061.  
  1062.         Reset();
  1063.  
  1064.         DrawCursor();
  1065.     }
  1066. }
  1067.  
  1068.     /* OpenEmulator(STRPTR Name):
  1069.      *
  1070.      *    Open an emulation library.
  1071.      */
  1072.  
  1073. BYTE __regargs
  1074. OpenEmulator(STRPTR Name)
  1075. {
  1076.     CloseEmulator();
  1077.  
  1078.     XEM_HostData . Source        = NULL;
  1079.     XEM_HostData . Destination    = NULL;
  1080.     XEM_HostData . InESC        = FALSE;
  1081.     XEM_HostData . InCSI        = FALSE;
  1082.  
  1083.     if(XEmulatorBase = OpenLibrary(Name,0))
  1084.     {
  1085.         ClearCursor();
  1086.  
  1087.         Reset();
  1088.  
  1089.         if(SetupEmulator())
  1090.         {
  1091.             SetMask(RPort,DepthMask);
  1092.  
  1093.             ClearSerial();
  1094.  
  1095.             if(XEmulatorSetup(XEM_IO))
  1096.             {
  1097.                 RestartSerial(FALSE);
  1098.  
  1099.                 SetEmulatorOptions(XEM_PREFS_LOAD);
  1100.  
  1101.                 if(XEmulatorOpenConsole(XEM_IO))
  1102.                 {
  1103.                     STRPTR LibName = FilePart(XEmulatorBase -> lib_Node . ln_Name);
  1104.  
  1105.                     strcpy(EmulationName,&LibName[3]);
  1106.  
  1107.                     EmulationName[strlen(EmulationName) - 8] = 0;
  1108.  
  1109.                     SetupXEM_MacroKeys(MacroKeys);
  1110.  
  1111.                     return(TRUE);
  1112.                 }
  1113.             }
  1114.             else
  1115.                 RestartSerial(FALSE);
  1116.         }
  1117.  
  1118.         DrawCursor();
  1119.  
  1120.         CloseLibrary(XEmulatorBase);
  1121.     }
  1122.  
  1123.     strcpy(EmulationName,LocaleString(MSG_TERMXEM_NO_EMULATION_TXT));
  1124.  
  1125.     XEmulatorBase    = NULL;
  1126.     XEM_Signal    = NULL;
  1127.  
  1128.     return(FALSE);
  1129. }
  1130.  
  1131.     /* xOn():
  1132.      *
  1133.      *    Small local routine, complements XOff().
  1134.      */
  1135.  
  1136. STATIC VOID
  1137. xOn(VOID)
  1138. {
  1139.     UBYTE c = XON;
  1140.  
  1141.     if(Config -> SerialConfig -> xONxOFF)
  1142.         Status = STATUS_HOLDING;
  1143.  
  1144.     if(Config -> SerialConfig -> PassThrough)
  1145.         SerWrite(&c,1);
  1146. }
  1147.  
  1148.     /* xOff():
  1149.      *
  1150.      *    Small local routine, complements XOn() in Serial.c
  1151.      */
  1152.  
  1153. STATIC VOID
  1154. xOff(VOID)
  1155. {
  1156.     UBYTE c = XOF;
  1157.  
  1158.     if(Status == STATUS_HOLDING)
  1159.         Status = STATUS_READY;
  1160.  
  1161.     if(Config -> SerialConfig -> PassThrough)
  1162.         SerWrite(&c,1);
  1163. }
  1164.  
  1165.     /* SetupXEM_MacroKeys(struct MacroKeys *Keys):
  1166.      *
  1167.      *    Sets up the internal representation of the macro key
  1168.      *    data to fit the XEM specification.
  1169.      */
  1170.  
  1171. VOID __regargs
  1172. SetupXEM_MacroKeys(struct MacroKeys *Keys)
  1173. {
  1174.         /* Are we allowed to do what we want to do? */
  1175.  
  1176.     if(XEM_MacroKeys && XEmulatorBase && Config -> TerminalConfig -> EmulationMode == EMULATION_EXTERNAL)
  1177.     {
  1178.         WORD i,j,k = 0;
  1179.  
  1180.             /* Clear the macro list. */
  1181.  
  1182.         NewList(&XEM_MacroList);
  1183.  
  1184.             /* Run down the list of qualifiers. */
  1185.  
  1186.         for(i = XMKQ_NONE ; i <= XMKQ_CONTROL ; i++)
  1187.         {
  1188.                 /* Run down the function keys. */
  1189.  
  1190.             for(j = 0 ; j < 10 ; j++)
  1191.             {
  1192.                     /* If the key has no data attached,
  1193.                      * don't use it in the list.
  1194.                      */
  1195.  
  1196.                 if(Keys -> Keys[i][j][0])
  1197.                 {
  1198.                     XEM_MacroKeys[k] . xmk_Type        = XMKT_RAWKEY;
  1199.                     XEM_MacroKeys[k] . xmk_Qualifier    = i;
  1200.                     XEM_MacroKeys[k] . xmk_Code        = 0x50 + j;
  1201.                     XEM_MacroKeys[k] . xmk_UserData        = NULL;
  1202.  
  1203.                     AddTail(&XEM_MacroList,(struct Node *)&XEM_MacroKeys[k++]);
  1204.                 }
  1205.             }
  1206.         }
  1207.  
  1208.             /* Take care of the rest, add support for the xON key. */
  1209.  
  1210.         XEM_MacroKeys[k] . xmk_Type         = XMKT_COOKED;
  1211.         XEM_MacroKeys[k] . xmk_Qualifier    = NULL;
  1212.         XEM_MacroKeys[k] . xmk_Code        = XON;
  1213.         XEM_MacroKeys[k] . xmk_UserData        = (APTR)xOn;
  1214.  
  1215.         AddTail(&XEM_MacroList,(struct Node *)&XEM_MacroKeys[k++]);
  1216.  
  1217.             /* Take care of the xOFF key. */
  1218.  
  1219.         XEM_MacroKeys[k] . xmk_Type         = XMKT_COOKED;
  1220.         XEM_MacroKeys[k] . xmk_Qualifier    = NULL;
  1221.         XEM_MacroKeys[k] . xmk_Code        = XOF;
  1222.         XEM_MacroKeys[k] . xmk_UserData        = (APTR)xOff;
  1223.  
  1224.         AddTail(&XEM_MacroList,(struct Node *)&XEM_MacroKeys[k]);
  1225.  
  1226.             /* Make the emulator notice the new settings. */
  1227.  
  1228.         XEmulatorMacroKeyFilter(XEM_IO,&XEM_MacroList);
  1229.     }
  1230. }
  1231.